#include <Arduino.h>
#include "Adafruit_NeoPixel.h"

Adafruit_NeoPixel pixel(1, 21, NEO_GRB + NEO_KHZ800);

void led_init()
{
#ifdef LED_PIN
    pinMode(LED_PIN,OUTPUT);
    digitalWrite(LED_PIN,HIGH);  
#endif    
#ifdef WS2812
    pixel.begin();
    pixel.clear();
    pixel.setPixelColor(0,pixel.Color(0, 100, 0));
    pixel.show();
#endif
}

void led_control(uint8_t index)
{ 
#ifdef LED_PIN
    if(index==0)
    {
        digitalWrite(LED_PIN,HIGH);
    }
    else
    {
        digitalWrite(LED_PIN,LOW);
    }
#endif
#ifdef WS2812
    if(index==0)
    {
        pixel.clear();
        pixel.show();
    }
    else
    {
        pixel.setPixelColor(0,pixel.Color(0, 0, 100));
        pixel.show();
    }
#endif
}

int hex_to_str(uint8_t * buf, int len, char * str)
{
    int i=0;
    uint8_t temp;

    for(i=0;i<len;i++)
    {
        temp = buf[i] >> 4;
        if(temp < 10)
        {
            temp += '0'; 
        }
        else
        {
            temp += 'A' - 10;
        }
        str[i*2] = temp;

        temp = buf[i] & 0x0F;
        if(temp < 10)
        {
            temp += '0'; 
        }
        else
        {
            temp += 'A' - 10;
        }
        str[i*2 + 1] = temp;
    }

    return len*2;
}

int str_to_hex(char * str, int len, uint8_t * buf)
{
    int i=0;
    uint8_t temp;
    uint8_t low;
    uint8_t high;
    uint8_t data;
    int data_len = len / 2;

    for(i=0;i<data_len;i++)
    {
        high = 0;
        temp = str[2*i];
        if(temp >= '0' && temp <='9')
        {
            high = temp - '0';
        }
        else if(temp >= 'A' && temp <='F')
        {
            high = temp - 'A' + 10;
        }
        else if(temp >= 'a' && temp <='f')
        {
            high = temp -'a' + 10;
        }

        low = 0;
        temp = str[2*i + 1];
        if(temp >= '0' && temp <='9')
        {
            low = temp -'0';
        }
        else if(temp >= 'A' && temp <='F')
        {
            low = temp - 'A' + 10;
        }
        else if(temp >= 'a' && temp <='f')
        {
            low = temp -'a' + 10;
        }

        data = (high<<4) + low;

        buf[i] = data;
    }

    return data_len;
}
